package com.tjcu.service.impl;

import com.tjcu.dao.StudentDao;
import com.tjcu.entity.City;
import com.tjcu.entity.Mark;
import com.tjcu.entity.Student;
import com.tjcu.service.StudentService;
import com.tjcu.utils.MybatisUtil;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author 王恒杰
 * @version 1.0
 * @date 2021/11/3 14:55
 * @email 1078993387@qq.com
 * @Address 天津
 * @Description:
 */
public class StudentServiceImpl implements StudentService {

    @Override
    public List showPage(Integer pageNumber) {
        //调用dao层接口方法
        StudentDao mapper = (StudentDao) MybatisUtil.getMapper(StudentDao.class);
        //一页有多少行
        int rows=3;
        //当前页的起始条数
        int page=(pageNumber-1)*rows;
        List students = mapper.selectSCCGMByPage(page, rows);
        //关闭资源
        MybatisUtil.close();
        return students;
    }

    @Override
    public Integer selectTotalPage() {
        //调用dao层接口方法
        StudentDao mapper = (StudentDao) MybatisUtil.getMapper(StudentDao.class);
        //一页有多少行
        int rows=3;
        Integer count = mapper.selectTotalCount();
        int totalPage=0;
        if(count%rows==0){
            totalPage=count/rows;
        }else {
            totalPage=count/rows+1;
        }
        //关闭资源
        MybatisUtil.close();
        return totalPage;
    }

    @Override
    public void addStudent(Student student) {
        //调用dao层接口方法
        StudentDao mapper = (StudentDao) MybatisUtil.getMapper(StudentDao.class);
        //先插入数据获取插入学生的id
        mapper.insertStudent(student);
        //获取当前学生的标签
        List<Mark> marks = student.getMarks();
        System.out.println(marks);
        for (Mark mark : marks) {
            System.out.println("查询到的学生id"+student.getSid());
            System.out.println(mark.getMid());
            //插入进学生和标签的挂心表中
            mapper.insertSm(mark.getMid(),student.getSid());
        }

        //关闭资源,提交事务
        MybatisUtil.commit();
    }

    @Override
    public void updateStudent(Student student) {
        //调用dao层接口方法
        StudentDao mapper = (StudentDao) MybatisUtil.getMapper(StudentDao.class);
        //先用学生id删除关联表中的学生和标签关联
        mapper.deleteSm(student.getSid());

        mapper.updateStudent(student);

        //获取当前学生的标签
        List<Mark> marks = student.getMarks();
        System.out.println(marks);
        for (Mark mark : marks) {
            System.out.println("查询到的学生id"+student.getSid());
            System.out.println(mark.getMid());
            //插入进学生和标签的挂心表中
            mapper.insertSm(mark.getMid(),student.getSid());
        }


        //关闭资源,提交事务
        MybatisUtil.commit();
    }

    @Override
    public Student selectStudentById(Integer sid) {
        //调用dao层接口方法
        StudentDao mapper = (StudentDao) MybatisUtil.getMapper(StudentDao.class);
        Student student = mapper.selectStudentById(sid);
        //关闭资源,提交事务
        MybatisUtil.commit();
        return student;
    }

    @Override
    public void dropStudent(Integer sid) {
        //调用dao层接口方法
        StudentDao mapper = (StudentDao) MybatisUtil.getMapper(StudentDao.class);
        //先用学生id删除关联表中的学生和标签关联
        mapper.deleteSm(sid);
        mapper.deleteStudent(sid);
        //关闭资源,提交事务
        MybatisUtil.commit();
    }

    @Override
    public List<Student> showByNameOrPhoneOrQQ(Student student) {
        //调用dao层接口方法
        StudentDao mapper = (StudentDao) MybatisUtil.getMapper(StudentDao.class);

        List students = mapper.selectByNameOrPhoneOrQQ(student);
        //关闭资源
        MybatisUtil.close();
        return students;
    }

    @Override
    public List showCnameAndGName() {
        //调用dao层接口方法
        StudentDao mapper = (StudentDao) MybatisUtil.getMapper(StudentDao.class);
        List list = mapper.selectCnameAndGName();
        //关闭资源
        MybatisUtil.close();
        return list;
    }

    @Override
    public List<Mark> showMarkName() {
        //调用dao层接口方法
        StudentDao mapper = (StudentDao) MybatisUtil.getMapper(StudentDao.class);
        List<Mark> marks = mapper.selectMarkName("学生");
        //关闭资源
        MybatisUtil.close();
        return marks;
    }

    @Override
    public List<City> showCityName() {
        //调用dao层接口方法
        StudentDao mapper = (StudentDao) MybatisUtil.getMapper(StudentDao.class);
        List<City> cities = mapper.selectCityName();
        //关闭资源
        MybatisUtil.close();
        return cities;
    }

    @Override
    public void addSm(Integer sid, Integer mid) {
        //调用dao层接口方法
        StudentDao mapper = (StudentDao) MybatisUtil.getMapper(StudentDao.class);
        mapper.insertSm(sid,mid);
        //关闭资源
        MybatisUtil.commit();

    }
}
